package com.ikingtech.platform.service.system.label.service;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.label.model.LabelQueryParamDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.system.label.entity.LabelDO;
import com.ikingtech.platform.service.system.label.mapper.LabelMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author tie yan
 */
@Service
@AllArgsConstructor
@Slf4j
public class LabelService extends ServiceImpl<LabelMapper, LabelDO> {

    public Boolean nameExist(String name) {
        return this.baseMapper.exists(Wrappers.<LabelDO>lambdaQuery().isNotNull(LabelDO::getId).eq(LabelDO::getName, name));
    }

    public boolean nameExist(String id, String name) {
        return this.baseMapper.exists(Wrappers.<LabelDO>lambdaQuery().ne(LabelDO::getId, id).eq(LabelDO::getName, name));
    }

    public boolean exist(String id) {
        return this.baseMapper.exists(Wrappers.<LabelDO>lambdaQuery().eq(LabelDO::getId, id));
    }

    /**
     * 标签分页查询
     *
     * @param queryParam 分页查询结果
     * @return 查询结果
     */
    public PageResult<LabelDO> listPage(LabelQueryParamDTO queryParam) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()),
                Wrappers.<LabelDO>lambdaQuery()
                        .like(Tools.Str.isNotBlank(queryParam.getName()), LabelDO::getName, queryParam.getName())
                        .orderByDesc(LabelDO::getCreateTime)));
    }
}
